﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace Fast.AspNetCore.WebApi.Tests.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]"), ApiExplorerSettings(GroupName = "Othor")]
    public class RemoteRequestController : ControllerBase
    {
        private List<RemoteRequestTest> list;

        private readonly ILogger<RemoteRequestController> _logger;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        public RemoteRequestController(ILogger<AuthTestController> logger)
        {
            list = new List<RemoteRequestTest>();
            for (int i = 1; i <= 10; i++)
            {
                list.Add(new RemoteRequestTest
                {
                    Id = i,
                    Name = $"Name_{i}",
                    Address = $"Address_{i}"
                });
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetAsync()
        {
            await Task.Delay(10000);
            return await Task.Run(() => Ok(list.FirstOrDefault()));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetByIdAsync([FromQuery] int id)
        {
            return await Task.Run(() => Ok(list.FirstOrDefault(p => p.Id == id)));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetByEntityAsync([FromQuery] RemoteRequestTest requestTest)
        {
            return await Task.Run(() => Ok(list.FirstOrDefault(p => p.Id == requestTest.Id)));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> PostAsync()
        {
            return await Task.Run(() => Ok());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> PostFromBodyAsync([FromBody] RemoteRequestTest requestTest)
        {
            var data = list.FirstOrDefault(p => p.Id == requestTest.Id);
            if (data == null)
            {
                return await Task.Run(() => BadRequest());
            }
            return await Task.Run(() => Ok(data));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> PostFromFormAsync([FromForm] RemoteRequestTest requestTest)
        {
            var data = list.FirstOrDefault(p => p.Id == requestTest.Id);
            if (data == null)
            {
                return await Task.Run(() => BadRequest());
            }
            return await Task.Run(() => Ok(data));
        }

        /// <summary>
        /// 携带头部信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> PostCarryHeadersAsync()
        {
            if (HttpContext.Request.Headers.TryGetValue("HeaderKey", out var header) && header == "HeaderValue")
            {
                return await Task.Run(() => Ok());
            }
            return await Task.Run(() => BadRequest());
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public class RemoteRequestTest
    {
        /// <summary>
        /// 
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Address { get; set; }
    }
}
